/*
 * @Author: song
 * @Description:线绘制
 * @Date: 2024年04月23日 星期二 14:04:23
 * @Modify:
 */

using Function.SerializedPropertyAttribute;
using UnityEditor;
using UnityEngine;

namespace SCore.Editor.WindowDraw
{
#if UNITY_EDITOR
    [CustomEditor(typeof(ELineView))]
    public class EditorLineEditor : ObjectDrawEditor<ELineView>
    {
    }
#endif


    [CreateAssetMenu(fileName = "ELineView", menuName = "ScriptableObject/WindowEditor/Line", order = 3)]
    public class ELineView : ECommonView
    {
        [Head("垂直还是水平")] public MouseCursor _cursor;
        [Head("当前百分比")] public float _curPercent;
        [Head("颜色")] public Color _color;
        [Head("线宽")] public float _width;
        [Head("最小百分比")] public float _minPercent;
        [Head("最大百分比")] public float _maxPercent;
        [Head("偏移")] public Vector2 _offest;


        protected override bool MouseDrag(Event evt)
        {
            if (evt.button == 0)
            {
                if (_cursor == MouseCursor.ResizeHorizontal)
                {
                    _curPercent = Mathf.Clamp((evt.mousePosition.x - GetPosition().x) / GetPosition().width,
                        _minPercent / 100f,
                        _maxPercent / 100f) * 100f;
                }
                else if (_cursor == MouseCursor.ResizeVertical)
                {
                    _curPercent = Mathf.Clamp((evt.mousePosition.y - GetPosition().y) / (GetPosition().height),
                        _minPercent / 100f,
                        _maxPercent / 100f) * 100f;
                }

                return true;
            }

            return false;
        }

        public override void OnGUI()
        {
            var position = GetPosition();
            if (_cursor == MouseCursor.ResizeHorizontal)
            {
                _rect = new Rect(_offest.x + position.x + position.width * _curPercent / 100f,
                    _offest.y + position.y,
                    _width - _offest.x,
                    position.height - _offest.y);
            }
            else if (_cursor == MouseCursor.ResizeVertical)
            {
                _rect = new Rect(_offest.x + position.x,
                    _offest.y + position.y + position.height * _curPercent / 100f,
                    position.width - _offest.x,
                    _width - _offest.y);
            }

            EditorGUIUtility.AddCursorRect(_rect, _cursor);
            EditorGUI.DrawRect(_rect, _color);
        }


        public Rect GetPosition()
        {
            if (_parentWindow == null)
            {
                return _ieSubWindow.GetPosition();
            }

            return _parentWindow.position;
        }

        public float GetCurPercent()
        {
            var curPer = _curPercent / 100f;
            return curPer;
        }
    }
}